//
//  InputViewController.m
//  e-nurse
//
//  Created by  on 10/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "InputViewController.h"
#import "RootViewController.h"
#import "ManualInputViewController.h"
#import "ServerProxy.h"
#import "User.h"
#import "ActivateUserViewController.h"

@interface InputViewController ()

-(void)reloadToolbar;

@end

@implementation InputViewController
@synthesize rootViewController = _rootViewController;
@synthesize toolbar = _toolbar;
@synthesize userNameBarItem = _userNameBarItem;
@synthesize loginBarItem = _loginBarItem;
@synthesize activateBarItem = _activateBarItem;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self reloadToolbar];
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    self.rootViewController = nil;
    self.toolbar = nil;
    self.userNameBarItem = nil;
    self.loginBarItem = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self reloadToolbar];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)reloadToolbar
{
    NSMutableArray* items = [[NSMutableArray alloc] init];
    if([[ServerProxy sharedInstance] isOnline])
    {
        self.userNameBarItem.title = [NSString stringWithFormat:NSLocalizedString(@"WELCOME_USER", nil), [ServerProxy sharedInstance].currentUser.name];
        [items addObject:self.userNameBarItem];
    }
    else if([ServerProxy sharedInstance].currentUser != nil && ![ServerProxy sharedInstance].currentUser.isActivated)
    {
        [items addObject:self.activateBarItem];
    }
    else
    {
        [items addObject:self.loginBarItem];
    }
    [self.toolbar setItems:items];
    [items release];    
}

- (void)loginButtonClicked:(id)sender
{
    [self.rootViewController showLoginView];
}

- (void)manulInputButtonClicked:(id)sender
{
    ManualInputViewController* manualInputVC = [[ManualInputViewController alloc] initWithNibName:@"ManualInputViewController" bundle:nil];
    [self presentModalViewController:manualInputVC animated:YES];
    [manualInputVC release];
}

- (void)activeUserButtonClicked:(id)sender
{
    ActivateUserViewController* activateUserViewController = [[ActivateUserViewController alloc] initWithNibName:@"ActivateUserViewController" bundle:nil];
    activateUserViewController.userName = [ServerProxy sharedInstance].currentUser.name;
    activateUserViewController.userId = [ServerProxy sharedInstance].currentUser.userId;
    activateUserViewController.apiKey = [[ServerProxy sharedInstance] getApiKey];
    [self.navigationController pushViewController:activateUserViewController animated:NO];
    
    [activateUserViewController release];    
}

@end
